package com.jingtai.platform.service.supply.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.common.utils.random.AESUtil;
import com.jingtai.common.utils.random.TokenUtil;
import com.jingtai.core.entity.supply.SupplyGoods;
import com.jingtai.core.mapper.supply.SupplyGoodsMapper;
import com.jingtai.platform.service.supply.SupplyGoodsService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 供应商商品表 impl
 *
 * @author sunlaiqian
 * @date 2020-12-02 18:31:32
 */
@Service
public class SupplyGoodsServiceImpl extends ServiceImpl<SupplyGoodsMapper, SupplyGoods> implements SupplyGoodsService {

    @Autowired
    private SupplyGoodsService supplyGoodsService;

    @Override
    public ResultUtil SupplyGoodslist(Integer page, Integer limit, String conditions){
        QueryWrapper<SupplyGoods> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,supplyGoodsService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addSupplyGoods(SupplyGoods supplyGoods) {
        String goodsCode = TokenUtil.generateUUID();
        supplyGoods.setGoodsCode(goodsCode);
        boolean save = supplyGoodsService.save(supplyGoods);
        return save ? new ResultUtil(ResultMapInfo.ADDSUCCESS,supplyGoods):new ResultUtil(ResultMapInfo.ADDSUCCESS);
    }

    @Override
    public ResultUtil getSupplyGoodsById(Long id) {
        SupplyGoods supplyGoods = supplyGoodsService.getById(id);
        return supplyGoods == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,supplyGoods);
    }

    @Override
    public ResultUtil delSupplyGoodsById(Long id) {
        SupplyGoods supplyGoods = new SupplyGoods();
        supplyGoods.setId(id);
        supplyGoods.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = supplyGoodsService.updateById(supplyGoods);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil editSupplyGoods(SupplyGoods supplyGoods) {
        boolean editState = supplyGoodsService.updateById(supplyGoods);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
